package ax.crypto

import java.security._
import java.io._

object ECDSASign extends ECDSASign{
    def getKeyPairFromJKS(jks_file:File,password: String,alias:String): (PrivateKey, PublicKey) ={
     val store = KeyStore.getInstance(KeyStore.getDefaultType)
     val fis = new FileInputStream(jks_file)
     val pwd = password.toCharArray()
     store.load(fis, pwd)
     val sk = store.getKey(alias, pwd)
     val cert = store.getCertificate(alias)
     (sk.asInstanceOf[PrivateKey],cert.getPublicKey())
  }

}

class ECDSASign extends SignFunc{
  def sign(privateKey: PrivateKey, message: Array[Byte]): Array[Byte] = {
     val s1 = Signature.getInstance("SHA1withECDSA");  
     s1.initSign(privateKey);  
     s1.update(message)
     s1.sign()    
  }
  
  def verify(signature: Array[Byte], message: Array[Byte], publicKey: PublicKey): Boolean ={
    val s2 = Signature.getInstance("SHA1withECDSA");  
    s2.initVerify(publicKey);  
    s2.update(message)
    s2.verify(signature);       
  }

}